﻿
using Xacml.Core.Interfaces;
using Xacml.Core.Runtime;

namespace Xacml.Core.Runtime.Functions
{
  public class MapFunction : FunctionBase
  {
    public override string Id
    {
      get
      {
        return "urn:oasis:names:tc:xacml:1.0:function:map";
      }
    }

    public override IDataType Returns
    {
      get
      {
        return (IDataType) null;
      }
    }

    public override IDataType[] Arguments
    {
      get
      {
        return new IDataType[2]
        {
          (IDataType) DataTypeDescriptor.Function,
          (IDataType) DataTypeDescriptor.Bag
        };
      }
    }

    public override EvaluationValue Evaluate(EvaluationContext context, params IFunctionParameter[] args)
    {
      IFunction function = args[0].GetFunction(0);
      if (!args[1].IsBag)
        return EvaluationValue.False;
      BagValue bagValue = new BagValue(args[1].GetType(context));
      foreach (object obj in args[1].Elements)
        bagValue.Add((object) function.Evaluate(context, (IFunctionParameter) new EvaluationValue(obj, args[1].GetType(context))));
      return new EvaluationValue((object) bagValue, args[1].GetType(context));
    }
  }
}
